package com.ck.core.util;

import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 
 * <p>Title: MD5Util</p>  
 * <p>Description: md5算法</p>  
 * @author huangf 
 * @date 2018年8月15日
 */
public class MD5Util {
    private static final String ALGORITHM = "MD5";
    
    public static String digest(String in) {
        return DatatypeConverter.printHexBinary(digest(in.getBytes(StandardCharsets.UTF_8))).toLowerCase();
    }
    
    private static byte[] digest(byte[] in) {
        try { 
            MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);
            messageDigest.reset();
            return messageDigest.digest(in);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        System.out.println(MD5Util.digest("ckclient/register@timestamp=1"));
    }
}
